﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Assignment3
{
    public class Maze
    {
        public List<Wall> walls = new List<Wall>();

        public Floor floor;

        public Texture2D floorTexture;

        public Texture2D northTexture;
        public Texture2D westTexture;
        public Texture2D eastTexture;
        public Texture2D southTexture;
        
        public BasicEffect effect;

        int[,] map = new int[10,11] {{5,0,5,1,1,1,5,1,1,1,5},
                                     {3,0,4,0,0,0,4,0,0,0,3},
                                     {3,0,4,0,4,0,4,0,4,0,3},
                                     {3,0,4,0,4,0,4,0,4,0,3},
                                     {3,0,4,0,4,0,4,0,4,0,3},
                                     {3,0,4,0,4,0,4,0,4,0,3},
                                     {3,0,4,0,4,0,4,0,4,0,3},
                                     {3,0,4,0,4,0,4,0,4,0,3},
                                     {3,0,0,0,4,0,0,0,4,0,3},
                                     {6,2,2,2,6,2,2,2,6,0,6}};

        public Maze(BasicEffect effect)
        {
            this.effect = effect;
            BuildMaze();
        }

        public void LoadTextures(ContentManager content)
        {
            northTexture = content.Load<Texture2D>("whale");
            eastTexture = content.Load<Texture2D>("brick");
            southTexture = content.Load<Texture2D>("concrete1");
            westTexture = content.Load<Texture2D>("concrete2");
            floorTexture = content.Load<Texture2D>("gravel");
        }

        public void BuildMaze()
        {
            floor = new Floor(new Vector3(20, 22, 2), new Vector3(-16f, 0.0f,-20f));

            for (int y = 0; y < map.GetLength(1); y++ )
            {
                for (int x = 0; x < map.GetLength(0); x++)
                {
                    switch (map[x, y])
                    {
                        case 0:
                            break;
                        case 1:
                            AddWall(WallDirection.East, new Vector2(x,y));
                            break;
                        case 2:
                            AddWall(WallDirection.West, new Vector2(x,y));
                            break;
                        case 3:
                            AddWall(WallDirection.South, new Vector2(x, y));
                            break;
                        case 4:
                            AddWall(WallDirection.North, new Vector2(x, y));
                            break;
                        case 5:
                            AddWall(WallDirection.North, new Vector2(x,y));
                            AddWall(WallDirection.West, new Vector2(x,y));
                            break;
                        case 6:
                            AddWall(WallDirection.South, new Vector2(x,y));
                            AddWall(WallDirection.East, new Vector2(x,y));
                            break;

                    }
                }
            }
        }

        public void AddWall(WallDirection direction, Vector2 position)
        {
            position *= -4;

            Wall w = new Wall(new Vector3(2,2,2), new Vector3(position.X, 0.0f, position.Y), direction);
            w.CreateBoundingBox();
            walls.Add(w);
        }

        public void HandleCollisions(ref Vector3 position, Vector3 lastPosition)
        {
            foreach (Wall w in walls)
                w.HandleCollision(ref position, lastPosition);

        }

        public void Update(BasicEffect effect)
        {
        }

        public void Draw(GraphicsDevice graphics)
        {
            floor.RenderShape(graphics, effect, floorTexture);

            foreach (Wall w in walls)
            {
                switch(w.direction)
                {
                    case WallDirection.North:
                        w.RenderShape(graphics, effect, northTexture, southTexture);
                        break;
                    case WallDirection.East:
                        w.RenderShape(graphics, effect, eastTexture, westTexture);
                        break;
                    case WallDirection.South:
                        w.RenderShape(graphics, effect, northTexture, southTexture);
                        break;
                    case WallDirection.West:
                        w.RenderShape(graphics, effect, eastTexture, westTexture);
                        break;
                }
            }   
        }
    }
}
